<?php

/*
 * This class is used for as Model for Resources Access table 
 */

class Model_Resources extends Model_Base {

    protected $_name = 'tbl_resources';
    protected $_primaryKey = 'res_id';

    public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry with the role type
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('resources' => 'tbl_resources'))
                ->where('resources_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchResourcespluginAccess($where = "", $limit = "", $offset = "0", $order = 'res_id', $sort = 'ASC', $group = '') {
        $select = $this->select()
                ->from(array('res' => 'tbl_resources'));
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        if ($group != "") {
            $select->group(array($group));
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchResglobalAccess($where = "", $limit = "", $offset = "0", $order = 'res_id', $sort = 'ASC', $group = '') {
        $select = $this->select()
                ->from(array('res' => 'tbl_resources'))
                ->where("res_listdisplay=1");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        if ($group != "") {
            $select->group(array($group));
        }
        $select->order($order . " " . $sort);
        #echo $select;exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get roles
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchResourcesAccess($where = "", $limit = "", $offset = "0", $order = 'res_id', $sort = 'ASC', $group = '') {
        $select = $this->select()
                ->from(array('res' => 'tbl_resources'))
                ->where("res_listdisplay!=1");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        if ($group != "") {
            $select->group(array($group));
        }
        $select->order($order . " " . $sort);
        #echo $select;exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getResourcesAccessCount($where) {
        $select = $this->select()
                ->from(array('res' => 'tbl_resources'), Array('cnt' => 'count(*)'))
                ->where("res_listdisplay!=1");
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    //join with tbl_role_access
    public function fetchResourcesRoleAccess($where = "", $limit = "", $offset = "0", $order = 'res_id', $sort = 'ASC', $group = 'res_id') {
        $select = $this->select()
                ->from(array('res' => 'tbl_resources'))
                ->setIntegrityCheck(false)
                ->join(array('role' => 'tbl_role_access'), "role.role_action_name = res.res_action_name")
                ->where("role.role_controller_name = res.res_controller_name");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        if ($group != "") {
            $select->group(array($group));
        }
        $select->order($order . " " . $sort);
        # echo $select."<br>";
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

}
